# from kubernetes import client,config
# import os
#
# kubeconfig = os.path.join(os.getcwd(), "config")
# config.load_kube_config(kubeconfig)
# apps_api = client.AppsV1Api()  # 资源接口实例化


from kubernetes import client
import os
configuration = client.Configuration()
configuration.host = "https://192.168.16.24:6443"  # APISERVER地址
#ca_file = os.path.join(os.getcwd(),"ca.crt") # K8s集群CA证书（/etc/kubernetes/pki/ca.crt）
#configuration.ssl_ca_cert= ca_file
configuration.verify_ssl = False  # 启用证书验证
token = "eyJhbGciOiJSUzI1NiIsImtpZCI6InZpMTgyeFdKWDFaYXVLMmdDUzNxdXFKeHBsYXg5WEtYMEVUR1RYRERveDgifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi10b2tlbi1zajV2biIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJhZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImY2NDFiNGJmLWVmMWItNDNhMy05Nzg1LTAwNmYzYzNhNWU2NCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDphZG1pbiJ9.GUVvM8NXvKcva-CsEoe54w7g_yDzChRLNUzh-EKMaBcogETILghbwvCG0UE8zqv0EmgGdGvt-RC6yNtT7adpmtcALTSeKJX_9kmnKpwAMDh86FH6rxWBiKGuf1r4H0DIZ0rON-HH869fhx5ZmZUl4gJCUqlaUjStvmKz5A72hzvqYtUYhrBMQxzt5KnhSbWQACpIJosUTBpvqc3-e4atrcNeofv3DtecjKFTyQ9ATEa6YwcqDXwL91m3pVGqj86t4AFXqjPZ9FfDTUppWabaKME4s-F_Lvrk06-aMhgwtHUhNsWDqDwOGyNF9mRE2dpRLqYRBA7K-NCqzJR5NKbBVg"
configuration.api_key = {"authorization": "Bearer" + token}
client.Configuration.set_default(configuration)
apps_api = client.AppsV1Api()
core_api = client.CoreV1Api()

# for dp in apps_api.list_deployment_for_all_namespaces().items:
#     print(dp.metadata.name)
"""
namespace = "default"
name = "api-test"
replicas = 3
labels = {'a':'1', 'b':'2'}  # 不区分数据类型，都要加引号
image = "nginx"
body = client.V1Deployment(
            api_version="apps/v1",
            kind="Deployment",
            metadata=client.V1ObjectMeta(name=name),
            spec=client.V1DeploymentSpec(
                replicas=replicas,
                selector={'matchLabels': labels},
                template=client.V1PodTemplateSpec(
                    metadata=client.V1ObjectMeta(labels=labels),
                    spec=client.V1PodSpec(
                        containers=[client.V1Container(
                            name="web",
                            image=image
                        )]
                    )
                ),
            )
        )
try:
    apps_api.create_namespaced_deployment(namespace=namespace, body=body)
except Exception as e:
    print(e)
    status = getattr(e, "status")
    if status == 400:
        print(e)
        print("格式错误")
    elif status == 403:
        print("没权限")
    elif status == 422:
        print("格式错误")

namespace = "default"
name = "api-test"
selector = {'a':'1', 'b':'2'}  # 不区分数据类型，都要加引号
port = 80
target_port = 80
type = "NodePort"
body = client.V1Service(
    api_version="v1",
    kind="Service",
    metadata=client.V1ObjectMeta(
        name=name
    ),
    spec=client.V1ServiceSpec(
        selector=selector,
        ports=[client.V1ServicePort(
            port=port,
            target_port=target_port
        )],
        type=type
    )
)
core_api.create_namespaced_service(namespace=namespace, body=body)
"""